package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.*;
import com.mt.roll.core.dal.service.*;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.params.RobotParam;
import com.mt.roll.wms.service.*;
import com.mt.roll.wms.vo.EquipmentLocationVo;
import com.mt.roll.wms.vo.LocationVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author weihongyang
 * @Date 2021/11/9 2:58 下午
 * @Version 1.0
 */
@Transactional
@Service
@Slf4j
public class RobotServiceImpl extends BaseService implements RobotService {

    @Autowired
    private StorageBoxCircInfoServiceBiz storageBoxCircInfoServiceBiz;

    @Autowired
    private LocationService locationService;

    @Autowired
    private EquipmentLocationService equipmentLocationService;

    @Autowired
    private EquipmentLocationServiceBiz equipmentLocationServiceBiz;

    @Autowired
    private CarTaskExecutionInfoService carTaskExecutionInfoService;

    @Autowired
    private CarTaskExecutionInfoServiceBiz carTaskExecutionInfoServiceBiz;

    @Autowired
    private LocationStorageSubServiceBiz locationStorageSubServiceBiz;

    @Autowired
    private WorkOrderExecuteServiceBiz workOrderExecuteServiceBiz;

    @Override
    public R<IdVo> stackerTask(RobotParam robotParam) {
        QueryWrapper<StorageBoxCircInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(robotParam.getStorageId()), StorageBoxCircInfo.STORAGE_BOX_ID, robotParam.getStorageId());
        //通过堆垛机的存储箱id得到存储箱所在库位的行和列
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getOne(wrapper);
//        LocationVo locationVo = locationService.getLayersAndColumnsByStorageId(robotParam.getStorageId());
        log.info("通过堆垛机的存储箱id得到存储箱所在库位的行和列"+ storageBoxCircInfo.toString());
        //通过工单号来得到工序的下一个设备
        QueryWrapper<WorkOrderExecute> workOrderExecuteQueryWrapper = new QueryWrapper<>();
        workOrderExecuteQueryWrapper.eq(StringUtils.isNotBlank(robotParam.getWorkOrderNo()), WorkOrderExecute.WORK_ORDER_NO, robotParam.getWorkOrderNo());
        WorkOrderExecute workOrderExecute = workOrderExecuteServiceBiz.getOne(workOrderExecuteQueryWrapper);

        //截取获得下一个设备
//        String substring = s.substring(s.indexOf("c") + 1, s.indexOf("c") + 2);
        //

        //通过下一个设备来找到仓位的存储箱为空的行和列
        List<EquipmentLocationVo> equipmentLocationVoList = equipmentLocationService.getLayersAndColumnsByEquipmentMark(robotParam);
        if (equipmentLocationVoList.isEmpty()) {
            log.error("下一个设备找不到对应的空存储箱的位置");
        }
        EquipmentLocationVo equipmentLocationVo = equipmentLocationVoList.get(0);
        String task = storageBoxCircInfo.getRgvMark()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            +"0"+equipmentLocationVo.getGoodsShelves()+equipmentLocationVo.getLocationLayers()+equipmentLocationVo.getLocationColumns()+
                "|"+equipmentLocationVo.getStorageCode()+","+storageBoxCircInfo.getRgvMark()+"1"+storageBoxCircInfo.getGoodsShelves()+storageBoxCircInfo.getLocationLayers()+storageBoxCircInfo.getLocationColumns()
                +"|"+storageBoxCircInfo.getStorageCode();
        log.info("发给rgv的任务号："+task);
        DefaultIdentifierGenerator defaultIdentifierGenerator = new DefaultIdentifierGenerator();
        Long aLong = defaultIdentifierGenerator.nextId(new StorageBoxCircInfo());

        storageBoxCircInfo.setLocationStorageSubId(aLong);
        storageBoxCircInfoServiceBiz.update(storageBoxCircInfo,new QueryWrapper<>());

        //对应的历史表也保存
        //做完再保存


        //把任务存到任务表中
        CarTaskExecutionInfo carTaskExecutionInfo = new CarTaskExecutionInfo();
        carTaskExecutionInfo.setTaskCode(task);
        carTaskExecutionInfo.setFStorageBoxId(equipmentLocationVo.getStorageBoxId());
        carTaskExecutionInfo.setIsEmpty(0);
        carTaskExecutionInfo.setCurrLocationId(equipmentLocationVo.getLocationId());
        carTaskExecutionInfo.setCurrLocation(equipmentLocationVo.getLocationName());
        carTaskExecutionInfo.setTargetLocationId(storageBoxCircInfo.getCurrLocationId());
        carTaskExecutionInfo.setTargetLocation(storageBoxCircInfo.getCurrLocation());
        carTaskExecutionInfo.setEquipmentId(storageBoxCircInfo.getEquipmentId());
        carTaskExecutionInfo.setFForkCode("0");
        carTaskExecutionInfo.setWStorageBoxId(storageBoxCircInfo.getStorageBoxId());
        carTaskExecutionInfo.setWIsEmpty(1);
        carTaskExecutionInfo.setWCurrLocationId(storageBoxCircInfo.getCurrLocationId());
        carTaskExecutionInfo.setWCurrLocation(storageBoxCircInfo.getCurrLocation());
        carTaskExecutionInfo.setWTargetLocationId(equipmentLocationVo.getLocationId());
        carTaskExecutionInfo.setWTargetLocation(equipmentLocationVo.getLocationName());
        carTaskExecutionInfo.setWForkCode("1");
        carTaskExecutionInfoServiceBiz.save(carTaskExecutionInfo);

        //任务历史表也保存
        //做完再保存

        //把占用的空箱子库位锁定   状态改变
        EquipmentLocation equipmentLocation = new EquipmentLocation();
        equipmentLocation.setId(equipmentLocationVo.getId());
        equipmentLocation.setStatus(1);
        equipmentLocationServiceBiz.save(equipmentLocation);


        //把基板存起来
        for (LocationStorageSub locationStorageSub : robotParam.getSubstrateIdList()) {
            locationStorageSub.setLocationStorageSubId(aLong);
            locationStorageSub.setLocationId(storageBoxCircInfo.getCurrLocationId());
            locationStorageSub.setLocationName(storageBoxCircInfo.getCurrLocation());
            locationStorageSub.setStorageBoxFid(robotParam.getStorageBoxFid());
        }

        locationStorageSubServiceBiz.saveBatch(robotParam.getSubstrateIdList());
        return successful(IdVo.builder().id(1L).build());
    }
}
